package com.flykoc.lightinggem.listener;

import com.flykoc.lightinggem.api.GemAPI;
import com.google.common.collect.Maps;
import org.bukkit.block.Furnace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.FurnaceBurnEvent;
import org.bukkit.inventory.ItemStack;

import java.util.Map;

/**
 * 熔炉煅烧监听
 *
 * @author Zoyn
 * @since 2017-10-28
 */
public class FurnaceBurnListener implements Listener {

    /**
     * 该map用于辨析FurnaceSmelt时所用的宝石
     */
    public static Map<Integer, ItemStack> fuelItem = Maps.newHashMap();

    @EventHandler
    @SuppressWarnings({"deprecation"})
    public void onFurnaceBurn(FurnaceBurnEvent e) {
        Furnace furnace = (Furnace) e.getBlock().getState();
        // 熔炼的物品
        ItemStack smelt = furnace.getInventory().getSmelting();
        // 燃料
        ItemStack fuel = furnace.getInventory().getFuel();

        // 判断物品与燃料部分
        if (GemAPI.isGem(fuel)) {
            if (GemAPI.isWeapon(smelt)) {

                fuelItem.put(e.getBlock().hashCode(), fuel);

                e.setBurning(true);
                e.setBurnTime(240);
                return;
            }
            // 当熔炼的物品不是特定的物品时不会进行煅烧
            e.setCancelled(true);
        }

    }
}
